// //获取应用实例
const app = getApp()

// 引入SDK核心类
import QQMapWX from '../../utils/qqmap-wx-jssdk.min.js'
// 实例化API核心类
const qqmapsdk = new QQMapWX({
	key: '55RBZ-SPDKD-CWC44-HTISE-SUVQZ-BYFMT'
});
Page({
	data: {
		bannerImg: [{
			url: "/assets/images/banner_1.png"
		}, {
			url: "/assets/images/banner_2.png"
		}, {
			url: "/assets/images/banner_3.png"
		}, {
			url: "/assets/images/banner_4.png"
		}, {
			url: "/assets/images/banner_5.png"
		}],
		gameList: [{
				id: 0,
				url: "/assets/images/game1.png"
			},
			{
				id: 1,
				url: "/assets/images/game2.png"
			},
			{
				id: 2,
				url: "/assets/images/game3.png"
			},
		],
		addressArr: [
			[113.953768, 22.53327, "粤美特大厦点位"],
			[114.05959, 22.546513, "深圳书城点位点位"],
			[114.097927, 22.549218, "红荔邮政点位"],
			[114.070666, 22.541528, "深圳国际创新中心c座点位"],
			[113.952494, 22.529351, "科技生态园5栋C座点位"],
			[113.952617, 22.530753, "科技生态园8栋A座点位"]
		],
		address: '',
		distance: '',
	},

	onLoad: function() {
		this.getLocationMes();
	},

	//计算两点间的距离
	getLocationMes() {
		let that = this;
		let distanceArr = [];
		this.data.addressArr.forEach((item, index) => {
			qqmapsdk.calculateDistance({
				to: [{
					latitude: item[1],
					longitude: item[0]
				}],
				success: function(res) {
					distanceArr.push(res.result.elements[0].distance);
					let minDistance = Math.min(...distanceArr);
					if (distanceArr.length == that.data.addressArr.length) {
						if (minDistance >= 1000) {
							that.setData({
								distance: `${(minDistance/1000).toFixed(2)}KM`
							})
						} else {
							that.setData({
								distance: `${minDistance}M`
							})
						}
						let i = distanceArr.indexOf(minDistance);
						let minAddress = that.data.addressArr[i][2];
						that.setData({
							address: minAddress
						})
					}
				}
			})
		})
	},

	moreGame() {
		wx.switchTab({
			url: '../game/game'
		})
	},

	handleClick(e) {
		let id = e.currentTarget.dataset.id;
		wx.navigateTo({
			url: '../playGame/playGame?id=' + id
		})
	}
})





